package com.ytjj.common.cmpp;

import com.huawei.insa2.comm.cmpp.message.CMPPSubmitMessage;
import com.huawei.insa2.comm.cmpp.message.CMPPSubmitRepMessage;
import com.huawei.insa2.util.Args;
import com.huawei.insa2.util.Cfg;
import com.huawei.smproxy.SMProxy;
import com.ytjj.common.constants.CMPPConstants;
import com.ytjj.common.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class SMProxyTools{
    private static volatile SMProxy smproxy = null;

    public static SMProxy getSMProxy() {
        if (null == smproxy) {
            synchronized (SMProxyTools.class) {
                if (null == smproxy) {
                    Map<String, String> params = new HashMap<>();
                    params.put("host", "183.240.203.138");
                    params.put("port", "7890");
                    params.put("source-addr", "400152");
                    params.put("shared-secret", "ktSrTiKC");
                    params.put("heartbeat-interval", "10");
                    params.put("reconnect-interval", "10");
                    params.put("heartbeat-noresponseout", "5");
                    params.put("transaction-timeout", "10");
                    params.put("version", "1");
                    params.put("debug", "true");
                    smproxy = new SMProxy(params);
                    return smproxy;
                }
            }
        }
        return smproxy;
    }

    public static boolean sendMsg(String[] phone, String msg) {

        SMProxy smProxy = getSMProxy();
        String[] dest_terminal_id = phone;
        byte[] msg_content= null;
        try {
            msg_content = msg.getBytes("GB2312");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        if (msg_content.length > 140) {
            List<byte[]> list = LongMessageByte.getLongByte(msg);
            for (int i = 0; i < list.size(); i++) {
                msg_content = list.get(i);
                CMPPSubmitMessage submitMsg = generateMessage(dest_terminal_id, msg_content);
                CMPPSubmitRepMessage sub = null;
                try {
                    sub = (CMPPSubmitRepMessage)smProxy.send(submitMsg);//这里的smProxy就是上面用单例创建的smProxy对象
                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(sub.getResult() == 0){
                    return true;
                }
            }
        }else {
            CMPPSubmitMessage submitMsg = generateMessage(dest_terminal_id, msg_content);
            CMPPSubmitRepMessage sub = null;
            try {
                sub = (CMPPSubmitRepMessage)smProxy.send(submitMsg);//这里的smProxy就是第2点中用单例创建的smProxy对象
            } catch (IOException e) {
                e.printStackTrace();
            }
            if(sub.getResult() == 0){
                return true;
            }
        }
        return false;
    }

    private static CMPPSubmitMessage generateMessage(String[] dest_terminal_id, byte[] msg_content) {
        CMPPSubmitMessage submitMsg = new CMPPSubmitMessage(
                1,//@pk_Total 相同msg_Id消息总条数,短短信这里是1
                1,//@pk_Number 相同msg_Id的消息序号
                1,//@registered_Delivery 是否要求返回状态报告
                1,//@msg_Level  信息级别
                "",// @service_Id 业务类型 用户自定义 用来分类查询
                2,//@fee_UserType 0对目的终端计费；1对源终端计费；2对SP计费;
                "",//@fee_Terminal_Id 被计费用户的号码
                0,//@tp_Pid GSM协议类型 一般文本的时候设0,铃声图片设1
                0,//@tp_Udhi GSM协议类型 0不包含头部信息 1包含头部信息
                15,//@msg_Fmt 消息格式
                "400152",//@msg_Src 消息内容来源 6位的企业代码,这里需修改
                "02",// @fee_Type 资费类别 一般为02：按条计信息费
                "0",//@fee_Code 资费代码(以分为单位)
                null,//@valid_Time 存活有效期
                null,//@at_Time 定时发送时间
                "400152",//@src_Terminal_Id 移动所提供的服务代码  此处需修改
                dest_terminal_id,//@dest_Terminal_Id 接收业务的MSISDN号码,就是接收短信的手机号,String数组
                msg_content,//@msg_Content 消息内容 byte[],发送的消息内容,需要转化为byte[]
                "" //预留
        );
        return submitMsg;
    }

    public static void main(String[] args) {
        String[] phones = new String[]{"18873058010"};
        List<String> reqPhones = new ArrayList<String>();
        for (int i = 0; i < phones.length; i++) {
            OperatorJudgeUtil.checkOperator(phones[i]);
            reqPhones.add(phones[i]);
        }
        String[] newPhones = new String[reqPhones.size()];
        reqPhones.toArray(newPhones);
        sendMsg(newPhones, "您的验证码是123456");
    }
}
